<?php
namespace addon\slide\controller;

use app\common\controller\AddonAdminBase;
use tpfcore\Core;
use think\Request;
// +----------------------------------------------------------------------
// | Author: yaoyihong <510974211@qq.com>
// +----------------------------------------------------------------------
/**
 * ============================================================================
 * 版权所有 2017-2077 tpframe工作室，并保留所有权利。
 * @link http://www.tpframe.com/
 * @copyright Copyright (c) 2017 TPFrame Software LLC
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！未经本公司授权您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 */
class Slide extends AddonAdminBase
{
    /*
      钩子
    */
    public function hook(){
        
    }
    public function index(){
      $this->addonTemplate('index',[
          'list'=>Core::loadAddonModel($this->name)->getSlide(["order"=>"id asc","paginate"  =>["rows"=>DB_LIST_ROWS]])
      ]);
    }
    // 添加
    public function add_edit(){
      if(IS_POST){
        $this->jump(Core::loadAddonModel($this->name)->saveSlide($this->param));
      }
      $this->addonTemplate('add_edit',[
        "list"=>isset($this->param['id'])?Core::loadAddonModel($this->name)->getSlide(["where"  =>["id"=>$this->param['id']]]):null,
        "categorys"=>Core::loadAddonModel("SlideCat")->getSlideCat()
      ]);
    }
    //删除 
    public function del(){
      $this->jump(Core::loadAddonModel($this->name)->delSlide(['id'=>$this->param['id']]));
    }
    public function upload(){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('imgFile');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH .DS.'data'.DS.'uploads'.DS);
        if($info){
            // 成功上传后 获取上传信息
           $file_url = '/data/uploads/'.date("Ymd",time()).'/'.$info->getFilename();
           
           echo json_encode(array('error' => 0, 'url' => $file_url));die;
        }else{
            // 上传失败获取错误信息
          echo  $file->getError();die;
        }
    }
}
